;
(function (call) {
    var main = {
        getDayObj: { 0: 'sunday', 1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', 4: 'Thursday', 5: 'Friday', 6: 'Saturday' },
        getMonth: { 0: 'Jan', 1: 'Feb', 2: 'Mar', 3: 'Apr', 4: 'May', 5: 'Jun', 6: 'Jul', 7: 'Aug', 8: 'Sept', 9: 'Oct', 10: 'Nov', 11: 'Dec' },
        currentType:"2",
        init: function () {
            var productInfo = $(".task-menu-list li.active").data('product');
            if(productInfo.fortime.indexOf("05:45") > -1){
                this.currentType = "1";
            }
            this.checkYWYBType();
            this.initView();
            this.bindEvt();
        },
        initView: function () {
            var userInfo = $("#userInfo").data('info');
            this.userInfo = userInfo;
            $("#dutyPerson").val(userInfo.smuser.name);
            var time = new Date();
            $("#publishTime").val(this.getDayObj[time.getDay()] + ' ' + this.getMonth[time.getMonth()] + ' ' + new Date(time).format('dd,yyyy'));
            this.queryCtoolClang();
            this.getYwybDataDetail();
        },
        checkYWYBType: function () {
            if (this.currentType === "1") {
                var $wp02 = $(".ywyb_main .wp02");
                $wp02.find(">span")[0].innerText = "The Weather of Daytime";
                var $wp07 = $(".ywyb_main .wp07");
                $wp07.find(">span")[0].innerText = "The Weather of Tonight";
                var $mt06 = $(".ywyb_main .mt06");
                var $mt11 = $(".ywyb_main .mt11");
                $(".ywyb_main .pr05").after($mt11);
                $(".ywyb_main .pr10").after($mt06);
            }
        },
        // 初始化英文预报
        initYwybData: function () {
            var that = this;
            var params = that.getParams();
            loading.add('.cafe-container');
            $.getJSON(api.external.getGdybEnDataByParam, params, function (res) {
                loading.remove();
                console.log(res);
                var data = res.resultData["117.21_39.07"];
                $(".ywyb_main #wp02").val(data.WEA[0][0]);
                $(".ywyb_main #wp07").val(data.WEA[0][1]);
                $(".ywyb_main #mt11").val(data.TMAX[0][1]);
                $(".ywyb_main #mt06").val(data.TMIN[0][1]);
                $(".ywyb_main #wd03").val(data.EDA[2][0]);
                $(".ywyb_main #wd08").val(data.EDA[2][1]);
                $(".ywyb_main #ws04").val(data.EDA[3][0]);
                $(".ywyb_main #ws09").val(data.EDA[3][1]);
                $.getJSON(api.external.getGdybDataByParamData, params, function (resCn) {
                    var dataCn = resCn.resultData["117.21_39.07"];
                    $(".ywyb_main #pr05").val(weatherUtil.getVal(dataCn.WEA[0][0]));
                    $(".ywyb_main #pr10").val(weatherUtil.getVal(dataCn.WEA[0][1]));
                })
            })
        },
        // 组装参数
        getParams: function () {
            var fcstTime = new Date().format('yyyyMMdd200000');
            if (this.currentType === "1") {
                fcstTime = new Date().format('yyyyMMdd080000');
            }
            var params = {
                fcstTime: fcstTime,
                element: 'TMAX,TMIN,EDA,WEA',
                lon: 117.21,
                lat: 39.07,
                mode: "GDMODE",
                startPeriod: 0,
                endPeriod: 24,
                interval: 12,
                count: 0,
                stationType: 0
            }
            return params;
        },
        // 发布英文预报
        publishYwybData: function () {
            var that = this;
            var productInfo = $(".task-menu-list li.active").data('product');
            var jsonStr = {
                "tempData": {
                    "curTagData": {
                        "i_ywyb01_date01": { "val": $("#publishTime").val() },
                        "i_ywyb01_wp02": { "val": $("#wp02").val() },
                        "i_ywyb01_wd03": { "val": $("#wd03").val() },
                        "i_ywyb01_ws04": { "val": $("#ws04").val() },
                        "i_ywyb01_pr05": { "val": $("#pr05").val() },
                        "i_ywyb01_mt06": { "val": $("#mt06").val() },
                        "i_ywyb01_wp07": { "val": $("#wp07").val() },
                        "i_ywyb01_wd08": { "val": $("#wd08").val() },
                        "i_ywyb01_ws09": { "val": $("#ws09").val() },
                        "i_ywyb01_pr10": { "val": $("#pr10").val() },
                        "i_ywyb01_mt11": { "val": $("#mt11").val() }
                    },
                }
            }
            if (productInfo.fortime == '16:40') {
                jsonStr = {
                    "tempData": {
                        "curTagData": {
                            "i_ywyb02_date01": { "val": $("#publishTime").val() },
                            "i_ywyb02_wp02": { "val": $("#wp02").val() },
                            "i_ywyb02_wd03": { "val": $("#wd03").val() },
                            "i_ywyb02_ws04": { "val": $("#ws04").val() },
                            "i_ywyb02_pr05": { "val": $("#pr05").val() },
                            "i_ywyb02_mt06": { "val": $("#mt06").val() },
                            "i_ywyb02_wp07": { "val": $("#wp07").val() },
                            "i_ywyb02_wd08": { "val": $("#wd08").val() },
                            "i_ywyb02_ws09": { "val": $("#ws09").val() },
                            "i_ywyb02_pr10": { "val": $("#pr10").val() },
                            "i_ywyb02_mt11": { "val": $("#mt11").val() }
                        },
                    }
                }
            }
            var params = {
                product_id: productInfo.productId,
                fortime: productInfo.fortime,
                forecasterId: that.userInfo.smuser.iid,
                jsonStr: JSON.stringify(jsonStr),
				taskId:productInfo.taskId
            }
            $.post(api.internal.ywyb.pushforeYWRB, params, function (res) {
                if (res.isSuccess) {
                    alert("发布成功");
                    that.getYwybDataDetail();
                    CafeJS.menuFunc.setTask();
                } else {
                    alert("发布失败");
                }
            })
        },
        // 获取英文预报数据
        getYwybDataDetail: function () {
            var that = this;
            var productInfo = $(".task-menu-list li.active").data('product');
            var time = new Date().format('yyyy-MM-dd 08:00:00');
            if (productInfo.fortime == '16:40') {
                time = new Date().format('yyyy-MM-dd 20:00:00');
            }
            var params = {
                product_id: productInfo.productId,
                fortime: productInfo.fortime,
                queryTime: time
            }
            $.getJSON(api.internal.common.getProductDetail, params, function (res) {
                if (!res.isSuccess) return;
                if (JSON.stringify(res.data) == "{}") {
                    that.initYwybData();
                } else {
                    that.getYwybData(res.data);
                }
            })
        },
        getYwybData: function (data) {
            var productInfo = $(".task-menu-list li.active").data('product');
            var item = JSON.parse(data.data.content)
            item = item.tempData.curTagData
            if (productInfo.fortime == '16:40') {
                $("#publishTime").val(item.i_ywyb02_date01.val)
                $("#wp02").val(item.i_ywyb02_wp02.val)
                $("#wd03").val(item.i_ywyb02_wd03.val)
                $("#ws04").val(item.i_ywyb02_ws04.val)
                $("#pr05").val(item.i_ywyb02_pr05.val)
                $("#mt06").val(item.i_ywyb02_mt06.val)
                $("#wp07").val(item.i_ywyb02_wp07.val)
                $("#wd08").val(item.i_ywyb02_wd08.val)
                $("#ws09").val(item.i_ywyb02_ws09.val)
                $("#pr10").val(item.i_ywyb02_pr10.val)
                $("#mt11").val(item.i_ywyb02_mt11.val)
            } else {
                $("#publishTime").val(item.i_ywyb01_date01.val)
                $("#wp02").val(item.i_ywyb01_wp02.val)
                $("#wd03").val(item.i_ywyb01_wd03.val)
                $("#ws04").val(item.i_ywyb01_ws04.val)
                $("#pr05").val(item.i_ywyb01_pr05.val)
                $("#mt06").val(item.i_ywyb01_mt06.val)
                $("#wp07").val(item.i_ywyb01_wp07.val)
                $("#wd08").val(item.i_ywyb01_wd08.val)
                $("#ws09").val(item.i_ywyb01_ws09.val)
                $("#pr10").val(item.i_ywyb01_pr10.val)
                $("#mt11").val(item.i_ywyb01_mt11.val)
            }
            this.productPreview(data.data.fileName);
        },
        // 产品预览
        productPreview: function (filePath) {
            $("#contentFrame").attr("src", "http://10.226.120.202:80//ioffice/officeView.do?path=" + encodeURI(encodeURI(filePath)));
        },
        queryCtoolClang: function () {
            // 天气现象
            var $wpSelect = $(".wpSelect").empty();
            $.post(api.internal.ywyb.queryCtoolClang, { classId: 99 }, function (res) {
                if (res.isSuccess && res.data) {
                    var data = res.data;
                    $("#wp02").val(data[0].cvalue);
                    $("#wp07").val(data[0].cvalue);
                    data.forEach(function (item) {
                        $wpSelect.append(`<span en="${item.cvalue}">${item.name}</span>`);
                    })
                }
            }, 'json');
            // 风向
            var $wdSelect = $(".wdSelect").empty();
            $.post(api.internal.ywyb.queryCtoolClang, { classId: 151 }, function (res) {
                if (res.isSuccess && res.data) {
                    var data = res.data;
                    $("#wd03").val(data[0].cvalue);
                    $("#wd08").val(data[0].cvalue);
                    data.forEach(function (item) {
                        $wdSelect.append(`<span en="${item.cvalue}">${item.name}</span>`);;
                    })
                }
            }, 'json');
            // 风力
            var $wsSelect = $(".wsSelect").empty();
            $.post(api.internal.ywyb.queryCtoolClang, { classId: 152 }, function (res) {
                if (res.isSuccess && res.data) {
                    var data = res.data;
                    $("#ws04").val(data[0].cvalue);
                    $("#ws09").val(data[0].cvalue);
                    data.forEach(function (item) {
                        $wsSelect.append(`<span en="${item.cvalue}">${item.name}</span>`);
                    })
                }
            }, 'json');
        },
        bindEvt: function () {
            var that = this;
            $("#publishBtn").click(function () {
                that.publishYwybData();
            })
            $("#returnBtn").click(function () {
                that.initYwybData();
            })
            $(".ywyb_main").on('click', 'li div span', function (e) {
                e.stopPropagation();
                $(this).addClass('active').siblings().removeClass('active');
                if ($(this).hasClass('active')) {
                    var txt = $(this).attr('en');
                    var val = $(this).parent().siblings('input').val();
                    if (!val) val = txt;
                    if (val.indexOf(txt) == -1)
                        val += ' ' + txt;
                    $(this).parent().siblings('input').val(val);
                    $(this).parent().hide();
                }
            }).on('focus', 'li input', function () {
                $(this).closest('li').siblings().find('div').hide();
                $(this).siblings('div').show();
            }).on('change', 'li input', function () {
                $(this).siblings('div').hide();
            }).on('click', 'li input', function (e) {
                e.stopPropagation();
            })
            $(".cafe-container").click(function () {
                $(".ywyb_main li div").hide();
            })
        },
    };
    if (typeof call === 'function') {
        call(main);
    }
})(function (main) {
    main.init();
})